/* $Id:culture_part.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "culture_part"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "culture_part.h"
#include "culture.h"
#include "../general/general.h"

culture_part::culture_part() {
    culture=0;
}

// Field : culture

class culture& culture_part::getCulture() const { if (culture==0) throw nullpointer(); return *culture; }
void culture_part::setCulture(class culture &v) { culture=&v; }

void culture_part::save(class channel &ch) {
    ch.write("culture_part", "");
    ch.write("c", getCulture().getName());
}

void culture_part::load(class channel &ch) {
    if (ch.key=="c" || ch.key=="culture") {
        if (!culture_repo.isCulture(ch.value)) {
            dbg_state("link unknown culture %s", ch.value.c_str());
            class culture rec;
            rec.setName(ch.value);
            culture_repo.setCulture(rec);
        }
        setCulture(culture_repo.getCulture(ch.value));
        ch.next();
    }
}

void culture_part::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<culture_part>" << endl;
    if (r<2) try {
        getCulture().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</culture_part>" << endl;
}
